package com.nodity.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Panel;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Painter extends JComponent implements Runnable, MouseListener, MouseMotionListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	Rectangle breadBoardBoundary;
	//MovableComponent boo=new MovableComponent(); 
	Point mouseCoords=new Point();
	myNode node;
	
	private boolean running;
	
	public class MovableComponent extends JComponent{
		private boolean canDrag=true;
		
		protected Point selectionPoint;
		JLabel label=new JLabel("Hello DUDE");
		protected Cursor cursorDrag=Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR);
		
		public MovableComponent(){
			addListeners();
			setOpaque(true);
			setBackground(Color.BLUE);
			
			label.setBackground(Color.WHITE);
			add(label,BorderLayout.WEST);
		}

		private void addListeners() {
			final MovableComponent object=this;
			addMouseMotionListener(new MouseAdapter() {
			
			@Override
			public void mouseMoved(MouseEvent e){
				selectionPoint=e.getPoint();
				setCursor(cursorDrag);
			}
			
			@Override
			public void mouseDragged(MouseEvent e){
				int selectX=selectionPoint.x;
				int selectY=selectionPoint.y;
				
				Point parent=getParent().getLocationOnScreen();
				Point mouseOnScreen=e.getLocationOnScreen();
				Point objectPos=new Point(mouseOnScreen.x-parent.x-selectX,mouseOnScreen.y-parent.y-selectY);
				setLocation(objectPos);
				
			}
			
			
			
			});
			
			
		}
		
		@Override
	    protected void paintComponent(Graphics g) {
	        super.paintComponent(g);
	        if (isOpaque()) {
	            g.setColor(getBackground());
	            g.fillRect(0, 0, getWidth(), getHeight());
	           
	        }
	    }
	}

	public Painter(){
		super();
		breadBoardBoundary=new Rectangle();
		breadBoardBoundary.setBounds(10, 10, getWidth()-20, getHeight()-20);

		node=new myNode("bob", new Point(100,100),500);
		System.out.println("New node created:"+node.toString()+", "+node.numeric.getNumericValue());
		
		addMouseMotionListener(this);
		addMouseListener(this);
		//add(boo);
		
		//setFocusable(true);
		//boo.setLocation(100,100);
		//boo.setVisible(true);
		Thread thread=new Thread(this);
		thread.start();

	}

	@Override
	public void paintComponent(Graphics g){
		super.paintComponent(g);

		drawBoundary(g);

		drawRectangle(g);

		drawStatusText(g);

		repaint();
	}

	public void drawStatusText(Graphics g) {

		g.drawString(String.valueOf(mouseCoords), 25, 25);
		g.drawString(String.valueOf(node.position.getLocation()), 25, 35);
	}

	public void drawRectangle(Graphics g) {
		node.display.repaint(g);

	}

	public void drawBoundary(Graphics g){
		breadBoardBoundary.setBounds(10, 10, getWidth()-20, getHeight()-20);

		g.drawRect(breadBoardBoundary.x,
				breadBoardBoundary.y,
				breadBoardBoundary.width,
				breadBoardBoundary.height);
	}

	@Override
	public void run() {
		
		Graphics g=getGraphics();

		running=true;
		while(running){
			
			updateStuff();
			drawEverything(g);
			
			try {
				Thread.sleep(10);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	private void drawEverything(Graphics g) {
		node.display.repaint(g);
		
	}

	private void updateStuff() {
		
		
	}
	
	private boolean hasPoint(Panel pnl, Point p ){
		
		
		int x=pnl.getLocation().x;
		int y=pnl.getLocation().y;
		int w=pnl.getWidth();
		int h=pnl.getHeight();
		boolean isValid=false;
		
		if((p.x>=x && p.x<=w+x) && (p.y>=y && p.y<=h+y)){
			isValid=true;
		}else{
			isValid=false;
		}
		
		
		return isValid;
		
	}

	@Override
	public void mouseDragged(MouseEvent e) {
		node.position.moveTo(e.getX(),e.getY());
		node.display.repaint(this.getGraphics());
	}

	@Override
	public void mouseMoved(MouseEvent e) {
		mouseCoords=e.getPoint();
		
	}

	@Override
	public void mouseClicked(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

}

class myNode extends AbstractNode{
	
	public PositionalNode position;
	public NumericalNode numeric;
	public RectangleAvatar display;
			
			public myNode(String name, Point pos, int value){
				super(name);
				position=new PositionalNode(pos);
				numeric=new NumericalNode(value);
				display=new RectangleAvatar(new Rectangle(pos.x,pos.y,50,50), Color.RED);
				display.rect.setBounds(position.x,position.y,50,50);
				
			}
		}
